package com.xiaoxx.utils;

import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.transport.ElasticsearchTransport;

import jakarta.json.stream.JsonGenerator;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;


@Slf4j
public class EsUtil {
    public static ByteArrayOutputStream printDSL(ElasticsearchTransport transport, SearchRequest searchRequest) {
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             JsonGenerator generator = transport.jsonpMapper().jsonProvider().createGenerator(baos)) {
            searchRequest.serialize(generator, transport.jsonpMapper());
            log.info("DSL: {}", searchRequest.toString());
            return baos;
        } catch (Exception e) {
            log.error("print SearchRequest DSL error: {}", e.getMessage(), e);
        }
        return null;
    }
}
